$(document).ready(function(){
	main.init();
	$.history.init(main.initHash);
})

var main = {
	__ot: null,
	objmenu: null,
	init: function(){
		//页面初始化
		document.title += " - " + global.title;
		$("#logo-navi").html(global.title);
		$("#username").html(userinfo.username + "（" + userinfo.role + "）");
		
		//初始化菜单
		var obj = {
			parent: "#menu",
			items: global.menulist,
			callback: main.menuCallback
		};
		objmenu = new ui_tab(obj);
		this.initHash();
		
		//注册浏览器分辨率调整时的页面调整措施
		if($.browser.msie){
			main.resize();
			$(window).bind("resize", main.resize);
		}
	},
	
	initHash: function(){
		//初始化菜单
		var link = document.location.hash;
		if(!link || link == ""){
			link = global.defatltmenu;
		}
		var indexs = main.getIndex(link,global.menulist);
		if(indexs != null){
			objmenu.changeItem(indexs);
		}
	},
	
	getIndex: function(link, data, indexs){
		//获取索引位置
		if(!indexs){
			indexs = [];
		}
		
		var item,indexs2;
		var len = indexs.length;
		for(var i=0;i<data.length;i++){
			item = data[i];
			indexs[len] = i;
			if(item.link && item.link == link){
				return indexs;
			}
			if(item.children){
				var indexs2 = this.getIndex(link, item.children, this.clone(indexs));
				if(indexs2 != null){
					return indexs2;
				}
			}
		}
		
		return null;
	},
	
	menuCallback: function(data,indexs){
		//菜单点击执行操作
		if(data.link && data.link.length > 0){
			var link = data.link.indexOf("#") != 0 ? data.link : data.link.substr(1).replace(/\|/ig, "/");
			var id= "iframe-" + indexs.join("-");
			var jq = $("#" + id);
			if(jq.length > 0){
				//已构建
				jq.show().siblings().hide();
				return;
			}
			
			var jqc = $("#container");
			if(jqc.children().length >= 5){
				jqc.children(":hidden").eq(0).remove();
			}
			jqc.children().hide();
			jqc.append('<iframe id="' + id + '" src="' + link + '" frameborder="0" allowTransparency="true" />');
		}
		
	},
	
	resize: function(){
		//浏览器宽高调整
		if(main.__ot != null){
			main.__ot = clearTimeout(main.__ot);
		}
		
		main.__ot = setTimeout(main.doresize, 200);
	},
	
	doresize: function(){
		//执行浏览器宽高调整
		$("#container").height(document.documentElement.clientHeight - $("#header").height() - $("#footer").height());
	},
	
	clone: function(obj) {
        //克隆对象
        if (obj != null && obj != undefined && typeof (obj) == "object") {
            if (obj instanceof Date) {
                return new Date(obj);
            }

            var d;
            if (obj instanceof Array) {
                d = [];
                for (var i = 0; i < obj.length; i++) {
                    d.push(this.clone(obj[i]));
                }
                return d;
            }

            var d = {};
            for (var k in obj) {
                d[k] = this.clone(obj[k]);
            }
        }
        else {
            return obj;
        }

        return d;
    }
}